package com.springboot.controller;

import com.springboot.dto.AssistantSeeDoctorsView;
import com.springboot.model.AssistantListFormKey;
import com.springboot.model.DoctorFieldForm;
import com.springboot.model.DoctorInfoForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.AssistantService;
import com.springboot.service.DoctorSetInfoViewService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * 本控制台用于助理查看医生列表
 */
@Controller
public class AssistantSeeDoctorsList {
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    DoctorSetInfoViewService doctorSetInfoViewService;
    @Autowired
    AssistantService assistantService;

    /**
     * 助理查看医生列表
     */
    @CrossOrigin
    @RequestMapping(value = "/getDoctorServlet")
    public @ResponseBody
    String assistantSeeDoctors(@RequestParam("tel") String tel)
    {
        System.out.println("查看医生列表");
        try {
            UserLoginForm user=new UserLoginForm();
            user=userLoginService.findUserByTel(tel);
            System.out.println("123123465789");
            Integer assistantId=user.getUserId();
            System.out.println(assistantId+"查看医生列表2");
            ArrayList<AssistantListFormKey> doctorId = new ArrayList<>();
            System.out.println(assistantId+"查看医生列表3");
            try {
                doctorId=assistantService.findDoctorsByID(assistantId);
                System.out.println(doctorId+"查看医生列表3");
            }
            catch (Exception e)
            {
                System.out.println("控制台");
            }
            System.out.println(assistantId+"查看医生列表3");
            ArrayList<AssistantSeeDoctorsView> assistantSeeDoctorsLists=new ArrayList<>();
            DoctorInfoForm doctorInfoForm = new DoctorInfoForm();
            DoctorFieldForm doctorFieldForm=new DoctorFieldForm();
            UserLoginForm userLoginForm=new UserLoginForm();

            System.out.println(assistantId+"查看医生列表4");
            for (int i=0;i<doctorId.size();i++)
            {
                Integer id=doctorId.get(i).getDoctorId();
                System.out.println(id+"DoctorId");
                userLoginForm=userLoginService.findUserById(id);
                String logoLink=userLoginForm.getLogoLink();
                String doctorTel=userLoginForm.getUserTel();
                System.out.println(logoLink+" || "+doctorTel);
                doctorFieldForm=doctorSetInfoViewService.findDoctorsByID(id);
                doctorInfoForm=doctorSetInfoViewService.findDoctorsByTel(doctorTel);
                System.out.println(doctorFieldForm.getDoctorId()+" "+doctorInfoForm.getDoctorId());
                AssistantSeeDoctorsView assistantSeeDoctorsView=new AssistantSeeDoctorsView();
                assistantSeeDoctorsView.setDoctorField(doctorFieldForm.getDoctorField());
                assistantSeeDoctorsView.setDoctorLogoLink(logoLink);
                assistantSeeDoctorsView.setDoctorTel(doctorTel);
                assistantSeeDoctorsView.setDoctorName(doctorInfoForm.getDoctorName());
                assistantSeeDoctorsView.setDoctorRank(doctorInfoForm.getDoctorRank());
                assistantSeeDoctorsLists.add(assistantSeeDoctorsView);
            }
            if(assistantSeeDoctorsLists.size()==0) {
                return "空";
            }
            else{
                System.out.println(assistantSeeDoctorsLists.toString());
                return assistantSeeDoctorsLists.toString();
            }
        }
        catch (Exception e)
        {
            return "False";
        }
    }
}